package com.zmm.util.convert;

import com.zmm.common.constant.NumberConstant;

/**
 * @author 900045
 * @description:
 * @name ByteUtil
 * @date By 2021-07-26 16:34:47
 */
public final class ByteUtil {
	
	/**
	 * 字节数组 转 字符串
	 * @author: 900045
	 * @date: 2021-07-26 16:38:51
	 * @throws 
	 * @param bytes: 
	 * @return: java.lang.String
	 **/
	public static String byteToString(byte[] bytes){
		
		if (NumberConstant.SIXTEEN != bytes.length) {
			return "** Bad UUID Format/Value **";
		}

		StringBuffer buf = new StringBuffer();

		for (int i = 0; i < NumberConstant.SIXTEEN; i++) {
			buf.append(Integer.toHexString(hiNibble(bytes[i])));
			buf.append(Integer.toHexString(loNibble(bytes[i])));
		}

		return buf.toString();
	}

	/**
	 * 获取 字节数组
	 * @author: 900045
	 * @date: 2021-07-26 16:42:59
	 * @throws 
	 * @param mloTag: 
	 * @param mhiTag: 
	 * @return: byte[]
	 **/
	public static byte[] toByteArray(long mloTag,long mhiTag) {
		byte[] bytes = new byte[16];
		int idx = 15;
		long val = mloTag;

		for (int i = 0; i < NumberConstant.EIGHT; i++) {
			bytes[idx--] = (byte) (int) (val & (long) 255);
			val >>= 8;
		}

		val = mhiTag;

		for (int i = 0; i < NumberConstant.EIGHT; i++) {
			bytes[idx--] = (byte) (int) (val & (long) 255);
			val >>= 8;
		}
		return bytes;
	}

	/**
	 * Return high order byte.
	 *
	 * @param b Object byte
	 * @return Result byte
	 */
	public static byte hiNibble(byte b) {
		return (byte) ((b >> 4) & 0xf);
	}

	/**
	 * Return low order byte.
	 *
	 * @param b Object byte
	 * @return Result byte
	 */
	public static byte loNibble(byte b) {
		return (byte) (b & 0xf);
	}
}
